﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Dycox.Reporting
{
    public class FileFilter
    {
        public FileFilter(string[] extensions, string description)
        {
            if (extensions == null || extensions.Length == 0)
                throw new ArgumentNullException("extensions");

            if (description == null)
                description = string.Join(";", extensions);
            _description = description;

            _extensions = extensions.Select(o => NormalizeExtension(o)).ToArray();
        }

        private static string NormalizeExtension(string ext)
        {
            ext = ext.Trim();
            int p = ext.IndexOf('.');

            if (p >= 0)
            {
                if (p == 1 && ext[0] == '*' || p == 0)
                    ext = ext.Substring(p + 1);
                else
                    throw new NotSupportedException();
            }
            
            return ext;
        }

        private string[] _extensions;

        public string[] Extensions
        {
            get { return _extensions; }
        }

        private string _description;

        public string Description
        {
            get { return _description; }
        }

        public override string ToString()
        {
            return _description + "|" + string.Join(";", _extensions.Select(o => "*." + o));
        }
    }
}
